<template>
   <div class="user-manage">
       <div class="query-form">
           <el-form ref="userForm" :inline="true" :model="user">
                <el-form-item label="用户名" prop="userName">
                    <el-input v-model="user.userName" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="handleQuery">查询</el-button>
                    <el-button @click="handleReset">重置</el-button>
                </el-form-item>
           </el-form>
       </div>
       <div class="base-table">
           <div class="action">
               <el-button type="primary" @click="handleCreate" v-has="'user-add'">新增</el-button>
               <el-button type="danger" @click="handlePatchDel">批量删除</el-button>
           </div>
           <el-table :data="userList" @selection-change="handleSelectionChange">
               <el-table-column type="selection" width="25" />
                <el-table-column 
                v-for="item in columns"
                :key="item.prop"
                :prop="item.prop" :label="item.label" :width="item.width" 
                :formatter="item.formatter"/>

                <el-table-column label="操作" width="120">
                    <template #default="scope">
                        <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button type="text" size="small" @click="handleDel(scope.row)">删除</el-button>
                    </template>
                    </el-table-column>
            </el-table>
            <el-pagination class="pagination" background layout="prev, pager, next" 
            :total="pager.total"
            :page-size="pager.pageSize"
            @current-change="handleCurrentChange"
            ></el-pagination>
       </div>

       <el-dialog title="用户新增" v-model="showModule">
           <el-form ref="userAddform" :model="userAdd" label-width="80px" :rules="rulesAdd">
               <el-form-item label="用户名" prop="userName">
                   <el-input v-model="userAdd.userName" placeholder="请输入用户名"/>
               </el-form-item>
               <el-form-item label="邮箱" prop="email">
                   <el-input v-model="userAdd.email" placeholder="请输入邮箱"/>
               </el-form-item>
               <el-form-item label="状态" prop="enable">
                   <el-switch v-model="userAdd.enable" active-text="启用" inactive-text="禁用"/>
               </el-form-item>
               <el-form-item label="系统角色" prop="roleList">
                    <el-select
                        v-model="userAdd.roleList"
                        placeholder="请选择用户系统角色"
                        multiple
                        style="width: 100%"
                    >
                        <el-option
                        v-for="role in roleList"
                        :key="role.id"
                        :label="role.name"
                        :value="role.id"
                        ></el-option>
                    </el-select>
                    </el-form-item>
           </el-form>

           <template #footer>
                <span class="dialog-footer">
                    <el-button @click="handleAddReset">取消</el-button>
                    <el-button type="primary" @click="handleAddUser">确认</el-button>
                </span>
            </template>
       </el-dialog>
   </div>
</template>
<script>
import {getCurrentInstance, onMounted, reactive, ref} from 'vue'
import { ElMessage } from 'element-plus'
export default {
    name:'user',
    setup(){
        // 生产环境下的ctx将访问不到
        const { proxy, ctx } = getCurrentInstance();
        const columns = reactive([
            {
                label:'ID',
                prop:'id'
            },
            {
                label:'用户名',
                prop:'userName'
            },
            {
                label:'邮箱',
                prop:'email'
            },
            {
                label:'状态',
                prop:'enable',
                formatter(row, column, cellValue, index){
                    // console.log('----------------cellValue------------' + cellValue)
                    return{
                        true:'启用',
                        false:'禁用'
                    }[cellValue]
                }
            },
            {
                label:'创建时间',
                prop:'createTime'
            }
        ]);
        const user = reactive({});
        const userAdd = reactive({enable:true});
        const userList = ref([]);
        const pager = reactive({
            page:1,
            pageSize:10
        })
        onMounted(()=>{
            getUserList();
            getRoleAllList();
        })
        const roleList = ref([])
        // 角色列表查询
        const getRoleAllList = async () => {
            const { code, result, message } = await proxy.$api.roleAllList();
            roleList.value = result;
        };
        const getUserList = async ()=>{
            let params = { ...user, ...pager };
            const { code, result, message } = await proxy.$api.userList(params)
            const {list, pageEl} = result;
            userList.value = list;
            pager.total = pageEl.total;
        }

        const handleQuery = ()=>{
            getUserList();
        }
        const handleReset = ()=>{
            ctx.$refs.userForm.resetFields();
        }
        const handleCurrentChange = (current)=>{
            pager.page = current;
            getUserList()
        }
        const handleDel = async (row)=>{
            await proxy.$api.userDel({
                userIds:row.id
            })
            ElMessage.success('删除成功')
            getUserList()
        }
        const handleSelectionChange = (list)=>{
            let arrIds = [];
            list.map(item=>{
                arrIds.push(item.id)
            })
            // console.log("------list", arrIds)
            checkedUserIds.value = arrIds;
        }
        const checkedUserIds = ref([])
        const handlePatchDel = async (row)=>{
            if(checkedUserIds.value.length == 0){
                ElMessage.error('至少选择一条用户')
                return;
            }
            const {code,message} = await proxy.$api.userDel({
                userIds:checkedUserIds.value.join()
            })
            if(code === 200){
                ElMessage.success(message)
                getUserList()
            }
        }
        const showModule = ref(false);
        const handleCreate = ()=>{
            showModule.value = true;
        }
        const rulesAdd = reactive({
            userName:[
                {
                    required:true,
                    message:'请输入用户名',
                    trigger:'blur'
                }
            ],
            email:[
                {
                    required:true,
                    message:'请输入邮箱',
                    trigger:'blur'
                }
            ]
        })
        const handleAddReset = ()=>{
            ctx.$refs.userAddform.resetFields();
            showModule.value = false;
        }
        const handleAddUser = async ()=>{
            // console.log('--------------------------' + JSON.stringify(userAdd))
            const {code,message} = await proxy.$api.userAdd(userAdd)
            if(code === 200){
                ElMessage.success(message)
                showModule.value = false;
                handleAddReset()
                getUserList()
            }
        }
        const handleEdit = async (row)=>{
            showModule.value = true;
            ctx.$nextTick(()=>{
                Object.assign(userAdd, row);
            });
        }
        return{
            user,
            userAdd,
            userList,
            columns,
            pager,
            checkedUserIds,
            showModule,
            rulesAdd,
            roleList,
            getUserList,
            handleQuery,
            handleReset,
            handleCurrentChange,
            handleDel,
            handlePatchDel,
            handleSelectionChange,
            handleCreate,
            handleAddReset,
            handleAddUser,
            handleEdit,
            getRoleAllList
        }
    }
}
</script>
<style lang="scss">

</style>